package com.tianyu.inspection.service.telemetry;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tianyu.inspection.common.Constants;
import com.tianyu.inspection.common.HttpRequest;
import com.tianyu.inspection.common.component.AppConstants;
import com.tianyu.inspection.common.model.JedisModel;
import com.tianyu.inspection.entity.Project;
import com.tianyu.inspection.entity.model.QueryMo;
import com.tianyu.inspection.mapper.FlyTaskMapper;
import com.tianyu.inspection.mapper.PipelineMapper;
import com.tianyu.inspection.mapper.UploadFileMapper;
import com.tianyu.inspection.service.api.IProjectService;
import com.tianyu.inspection.service.telemetry.yukong.YkClient;
import com.tianyu.inspection.service.websocket.WebSocketService;
import io.ocs.model.OcsSimpleModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;

import javax.annotation.Resource;
import java.util.Objects;

/**********************************
 * @Author YSW
 * @Description
 * @Date 2020.12.24 - 10:24
 * 47.104.142.82
 * 10.0.0.57
 **********************************/

@Slf4j
@Component
public class TelemetryAccessService {

    @Resource
    private WebSocketService webSocketService;

    @Resource
    private FlyTaskMapper flyTaskMapper;

    @Resource
    private PipelineMapper pipelineMapper;

    @Resource
    private UploadFileMapper uploadFileMapper;

    @Resource
    private IProjectService projectService;

    @Resource
    private OcsSimpleModel ocsSimpleModel;

    @Resource
    private JedisModel jedisModel;

    @Resource
    private AppConstants appConstants;

    public void start() {
        log.info("开启线程[多项目云平台多TCP客户端遥测下发、存储线程]...");
        QueryMo queryMo = new QueryMo();
        queryMo.put("state", 0);
        queryMo.setPageSize(-1);
        queryMo.build(Project.class, null);
        IPage<Project> projectPage = projectService.searchListPage(queryMo);
        Object dataList = projectPage.getRecords();
        JSONArray jsonArray = new ObjectMapper().convertValue(dataList, JSONArray.class);
        if (!jsonArray.isEmpty()) {
            for (Object object : jsonArray) {
                Project project = new ObjectMapper().convertValue(object, Project.class);
                String ykAccessKey = project.getYkAccessKey();
                String ykAccessSecret = project.getYkAccessSecret();
                Integer ykAccessAvailable = project.getYkAccessAvailable();
                Integer projectId = project.getId();
                if (ykAccessAvailable.equals(0) && Objects.nonNull(ykAccessKey) && Objects.nonNull(ykAccessSecret)) {
                    Runnable runnable = () -> new YkClient(
                            false,
                            projectId,
                            ykAccessKey,
                            ykAccessSecret,
                            webSocketService,
                            flyTaskMapper,
                            pipelineMapper,
                            uploadFileMapper,
                            projectService,
                            ocsSimpleModel,
                            jedisModel,
                            appConstants);
                    Constants.EXECUTOR.execute(runnable);
                    log.info("Project[" + projectId + "] 御空 Service Access Start...");
                }
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        JSONObject jsonArray = new JSONObject();
        jsonArray.put("KcKTHr1Dy4RuJ8AaHFIZ","MgiUAhScMHERPcC9SDK6DxhAPKC3SwK5");
        jsonArray.put("zngwdyFVKtJkId3R7xIa","kxrJA9VV2YZZHhqEfWc1DVFB8z8lSEfW");
        jsonArray.put("1uTczvMQjtDYNbJz6Ws6","M2178UdjiS6pDWH8acN2mepm5NT4b9av");

        jsonArray.forEach((key,value)->{
            Constants.EXECUTOR.execute(()->{
                JSONObject login = HttpRequest.cloudPlatformCompanyLogin("http://eapi.wogrid.com", key, (String) value);
                System.out.println(login);
            });
        });


        JSONObject jsonArray2 = new JSONObject();
        jsonArray2.put("s9yToQi9cJDn1jx5y7iIMC76","97nyrU2PzuenZYvKZzbuHQLgTPit9C");
        jsonArray2.put("C0w1JXScPia3Nh7QsEem59Qm","hB7mX2L9wjGns8HR8Oa0zqhiOcjIgr");

        jsonArray2.forEach((key,value)->{
            Constants.EXECUTOR.execute(()->{
                ModelMap login = HttpRequest.yuKongThirdLogin("https://third-api.yukong.live", key, (String) value);
                System.out.println(login);
            });
        });
    }
}
